home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / AptUrl / AptUrl.py next >
Encoding:
Python Source  |  2009-04-29  |  8.6 KB  |  245 lines

  1. # Copyright (c) 2007-2008 Canonical
  2. #
  3. # AUTHOR:
  4. # Michael Vogt <mvo@ubuntu.com>
  5. # With contributions by Siegfried-A. Gevatter <rainct@ubuntu.com>
  6. #
  7. # This file is part of AptUrl
  8. #
  9. # AptUrl is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License as published
  11. # by the Free Software Foundation; either version 2 of the License, or (at
  12. # your option) any later version.
  13. #
  14. # AptUrl is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. # General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with AptUrl; if not, write to the Free Software
  21. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. import sys
  24. import apt
  25. import apt_pkg
  26. import gettext
  27.  
  28. import aptsources.distro
  29. import Parser
  30. import Helpers
  31.  
  32. from aptsources.sourceslist import SourcesList, is_mirror
  33. from optparse import OptionParser
  34. from gettext import gettext as _
  35.  
  36. import os
  37. import os.path
  38.  
  39. # adding new repositories is currently disabled because some people have
  40. # security concerns about this feature
  41. allow_new_repositories = False
  42.  
  43. # channels that we know about
  44. channelsdir = "/usr/share/app-install/channels"
  45.  
  46. # return codes
  47. (RESULT_OK,
  48.  RESULT_CANCELT,
  49.  RESULT_ERROR,
  50.  RESULT_BADARGS) = range(4)
  51.  
  52. class AptUrlController(object):
  53.     
  54.     def __init__(self, ui):
  55.         self.ui = ui
  56.  
  57.     def enableSection(self, apturl):
  58.         added = False
  59.  
  60.         # parse sources.list
  61.         sourceslist = SourcesList()
  62.         distro = aptsources.distro.get_distro()
  63.         distro.get_sources(sourceslist)
  64.         
  65.         # check if we actually need to enable anything
  66.         requested_components = []
  67.         for component in apturl.section:
  68.             if (not component in distro.enabled_comps and not
  69.                 self.cache.has_key(apturl.package)):
  70.                 requested_components.append(component)
  71.         # if not, we are fine
  72.         if not requested_components:
  73.             return RESULT_OK
  74.         # otherwise ask the user if the really wants to anble them
  75.         if not self.ui.askEnableSections(apturl.section):
  76.             return RESULT_CANCELT
  77.         if not self.ui.doEnableSection(apturl.section):
  78.             self.ui.error(_("Enabling '%s' failed") % ", ".join(apturl.section))
  79.             return RESULT_ERROR
  80.         self.ui.doUpdate()
  81.         self.openCache()
  82.         return RESULT_OK
  83.  
  84.     def enableChannel(self, apturl):
  85.         # ensure that no funny path tricks can be played
  86.         # by e.g. passing "apt:foo?channel=../../"
  87.         channel = os.path.basename(apturl.channel)
  88.  
  89.         channelpath = "%s/%s.list" % (channelsdir,channel)
  90.         channelkey = "%s/%s.key" % (channelsdir,channel)
  91.  
  92.         # check
  93.         if not os.path.exists(channelpath):
  94.             self.ui.error(_("Unknown channel '%s'") % channel,
  95.                           _("The channel '%s' is not known") % channel)
  96.             return RESULT_ERROR
  97.         if not self.ui.askEnableChannel(apturl.channel):
  98.             return RESULT_CANCELT
  99.         if not self.ui.doEnableChannel(channelpath, channelkey):
  100.             self.ui.error(_("Enabling channel '%s' failed") % apturl.channel)
  101.             return RESULT_ERROR
  102.         self.ui.doUpdate()
  103.         self.openCache()
  104.         return RESULT_OK
  105.  
  106.     def openCache(self):
  107.         try:
  108.             self.cache = apt.Cache()
  109.         except SystemError, strerr:
  110.             if not '/etc/apt/sources.list' in str(strerr):
  111.                 raise
  112.             self.ui.error(_("Invalid /etc/apt/sources.list file"), strerr)
  113.             return False
  114.         if self.cache._depcache.BrokenCount > 0:
  115.             err_header = _("Software index is broken")
  116.             err_body = _("This is a major failure of your software " 
  117.                          "management system. Please check for broken packages "
  118.                          "with synaptic, check the file permissions and "
  119.                          "correctness of the file '/etc/apt/sources.list' and "
  120.                          "reload the software information with: "
  121.                          "'sudo apt-get update' and 'sudo apt-get install -f'."
  122.                          )
  123.             self.ui.error(err_header, err_body)
  124.             return False
  125.         return True
  126.     
  127.     def parseArgs(self):
  128.         parser = OptionParser()
  129.         parser.add_option("-p", "--http-proxy", dest="http_proxy",
  130.                           default=None, help="use http proxy")
  131.         (options, args) = parser.parse_args()
  132.  
  133.         # eval and add proxy
  134.         if options.http_proxy is not None:
  135.             proxy = options.http_proxy
  136.             if not ":" in proxy:
  137.                 proxy += ":3128"
  138.             os.environ["http_proxy"] = "http://%s" % proxy
  139.  
  140.         # parse
  141.         try:
  142.             apturl_list = Parser.parse(args[0])
  143.         except IndexError, e:
  144.             self.ui.error(_("Need a url to continue, exiting"))
  145.             return []
  146.         except Parser.InvalidUrlException, e:
  147.             self.ui.error(_("Invalid url: '%s' given, exiting") % sys.argv[1],
  148.                           "%s" % e)
  149.             return []
  150.         return (apturl_list)
  151.         
  152.     def verifyInstall(self, apturl):
  153.         " verify that the install package actually is installed "
  154.         # check if the package got actually installed
  155.         self.openCache()
  156.         pkg = self.cache[apturl.package]
  157.         if (not pkg.isInstalled or
  158.             pkg._pkg.CurrentState != apt_pkg.CurStateInstalled or
  159.             self.cache._depcache.BrokenCount > 0):
  160.             return False
  161.         return True
  162.  
  163.     def main(self):
  164.         # global return code
  165.         ret = RESULT_OK
  166.         ui = self.ui
  167.         
  168.         # parse arguments
  169.         apturl_list = self.parseArgs()
  170.         if not apturl_list:
  171.             return RESULT_BADARGS
  172.         
  173.         # open cache
  174.         if not self.openCache():
  175.             return RESULT_ERROR
  176.  
  177.         # now go over the url list
  178.         for apturl in apturl_list:
  179.             # FIXME: move this code block into a func like
  180.             #        evalAptUrl()
  181.  
  182.             if not apturl.schema in ("apt", "apt+http"):
  183.                 self.ui.error(_("Can not deal with protocol '%s' ") % apturl.schema)
  184.                 continue
  185.  
  186.             if apturl.section:
  187.                 if self.enableSection(apturl) != RESULT_OK:
  188.                     continue
  189.             elif apturl.channel:
  190.                 if self.enableChannel(apturl) != RESULT_OK:
  191.                     continue
  192.             elif apturl.refresh is not None:
  193.                 ui.doUpdate()
  194.                 if not self.openCache():
  195.                     return RESULT_ERROR
  196.             
  197.             # now check the package
  198.             if not self.cache.has_key(apturl.package):
  199.                 try:
  200.                     package_in_cache = bool(self.cache._cache[apturl.package])
  201.                 except KeyError:
  202.                     package_in_cache = False
  203.                 if package_in_cache:
  204.                     ui.error(_("Package '%s' is virtual.") % apturl.package)
  205.                     continue
  206.                 else:
  207.                     ui.error(_("Could not find package '%s'.") % apturl.package)
  208.                     continue
  209.             
  210.             if self.cache[apturl.package].isInstalled and apturl.minver is None:
  211.                 ui.message(_("Package '%s' is already installed") % apturl.package)
  212.                 continue
  213.  
  214.             # ask the user
  215.             pkg = self.cache[apturl.package]
  216.             (sum, desc, homepage) = Helpers.parse_pkg(pkg)
  217.             if not ui.askInstallPackage(apturl.package, sum, desc, homepage):
  218.                 ret = RESULT_CANCELT
  219.                 continue
  220.             
  221.             # try to install it
  222.             try:
  223.                 self.cache[apturl.package].markInstall()
  224.             except SystemError, e:
  225.                 ui.error(_("Can not install '%s' (%s) ") % (apturl.package, e))
  226.                 continue
  227.             if apturl.minver is not None:
  228.                 verStr = self.cache[apturl.package].candidateVersion
  229.                 if apt_pkg.VersionCompare(verStr, apturl.minver) < 1:
  230.                     ui.error(_("Package '%s' requests minimal version '%s', but "
  231.                                "only '%s' is available") % (apturl.package,
  232.                                                             apturl.minver,
  233.                                                             verStr))
  234.                     continue
  235.  
  236.             # install it
  237.             ui.doInstall(apturl)
  238.  
  239.             if not self.verifyInstall(apturl):
  240.                 ret = RESULT_ERROR
  241.  
  242.         # return values
  243.         return ret
  244.             
  245.